package com.uh.microservice.core.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.regex.Pattern;


/**
 * <p>
 * 公共方法类
 * </p>
 * <p>
 * 提供字符串处理的实用方法集
 * </p>
 */
public class StringUtil {
	public StringUtil() {
	}

	public static final String escapeForIntro(String string) {
		// String str = escapeHTMLTags(string);
		String str = string;
		str = replace(str, "\r\n", "<br>");
		str = replace(str, "\n", "<br>");
		str = replace(str, "'", "\\'");
		return replace(str, "\r", "");

	}

	/**
	 * 得到非空的字符串，若字符串对象为null，则返回""。
	 * 
	 * @param objValue
	 *            Object待转换的原字符串对象
	 * @return String 转换后的字符串
	 * */
	public static String getNotNullStr(Object objValue) {
		return (objValue == null ? "" : objValue.toString());
	}

	/**
	 * 得到非空的字符串，若字符串为null，则返回""。
	 * 
	 * @param strValue
	 *            String待转换的原字符串
	 * @return String 转换后的字符串
	 * */
	public static String getNotNullStr(String strValue) {
		return (strValue == null ? "" : strValue.trim());
	}

	// /**
	// * 使用正则表达式进行字符串内容替换
	// * @param regularExpression 正则表达式
	// * @param sub 替换的字符串
	// * @param input 要替换的字符串
	// * @return String 替换后的字符串
	// */
	// public static synchronized String regexReplace(String regularExpression,
	// String sub, String input)
	// {
	// Pattern pattern = PatternFactory.getPattern(regularExpression);
	// Matcher matcher = pattern.matcher(input);
	// StringBuffer sb = new StringBuffer();
	// while (matcher.find())
	// {
	// matcher.appendReplacement(sb, sub);
	// }
	// matcher.appendTail(sb);
	// return sb.toString();
	// }

	/**
	 * 判断一个字符串中是否包含符合正则表达式定义的匹配条件的子串
	 * 
	 * @param regularExpression
	 *            - 正则表达式
	 * @param input
	 *            - 待检查字符串
	 * @return - 若输入字符串中包含符合正则表达式定义的匹配条件的子串，则返回true，否则返回false
	 */
	// //正则表达式匹配判断
	// public static synchronized boolean exist(String regularExpression, String
	// input)
	// {
	// Pattern pattern = PatternFactory.getPattern(regularExpression);
	// Matcher matcher = pattern.matcher(input);
	// return matcher.find();
	// }

	/**
	 * 用"0"补足一个字符串到指定长度
	 * 
	 * @param str
	 *            - 源字符串
	 * @param size
	 *            - 补足后应达到的长度
	 * @return - 补零后的结果
	 */
	public static String fillZero(String str, int size) {
		String result;
		if (str.length() < size) {
			char[] s = new char[size - str.length()];
			for (int i = 0; i < (size - str.length()); i++) {
				s[i] = '0';
			}
			result = new String(s) + str;
		} else {
			result = str;
		}
		return result;
	}

	/**
	 * 根据字符串（文件类型或者多行输入类型）获取字符串数组
	 * 
	 * @param str1
	 *            String 输入字符串
	 * @return String[] 返回结果
	 */
	public static String[] getStrArryByString(String str1) {
		if (str1.indexOf("\t") > 0) {
			for (int i = 0; i < str1.length(); i++) {
				if (str1.substring(i, i + 1).equals("\t")) {
					str1 = str1.substring(0, i) + " "
							+ str1.substring(i + 1, str1.length());
				}
			}
		}
		StringTokenizer stringTokenizer = new StringTokenizer(str1, "\r\n");
		String[] strId = new String[stringTokenizer.countTokens()];
		int i = 0;
		while (stringTokenizer.hasMoreTokens()) {
			strId[i] = stringTokenizer.nextToken();
			i++;
		}
		return strId;
	}

	/**
	 * 判断一个字符串是否为 NUll 或为空
	 * 
	 * @param inStr
	 *            inStr
	 * @return boolean
	 */
	public static boolean isValid(String inStr) {
		if (inStr == null) {
			return false;
		} else if (inStr.equals("")) {
			return false;
		} else if (inStr.equals("null")) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 判断一个字符串是否为 NUll 或为空
	 * 
	 * @param inStr
	 *            inStr
	 * @return boolean
	 */
	public static boolean checkNotNull(String str) {
		boolean flag = false;

		if (str != null && str.trim().length() != 0)
			flag = true;
		return flag;
	}

	/**
	 * 获得指定长度的空格
	 * 
	 * @param spaceNum
	 *            spaceNum
	 * @return String
	 */
	public static String getStrSpace(int spaceNum) {
		return getStrWithSameElement(spaceNum, " ");
	}

	/**
	 * 获得指定长度的字符串
	 * 
	 * @param num
	 *            int
	 * @param element
	 *            String
	 * @return String
	 */
	public static String getStrWithSameElement(int num, String element) {
		if (num <= 0) {
			return "";
		}

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < num; i++) {
			sb.append(element);
		}
		return sb.toString();
	}

	/**
	 * 从右或左加空格
	 * 
	 * @param strIn
	 *            String
	 * @param totalLength
	 *            int
	 * @param isRightAlign
	 *            boolean
	 * @return String
	 */
	public static String getFillString(String strIn, int totalLength,
			boolean isRightAlign) {
		int spaceLength = 0;
		String spaces = null;
		String strOut = null;

		if (strIn == null) {
			strIn = "";
		}

		spaceLength = totalLength - strIn.length();

		if (spaceLength < 0) {
			spaceLength = 0;
		}
		spaces = StringUtil.getStrSpace(spaceLength);

		if (isRightAlign) {
			strOut = spaces + strIn;
		} else {
			strOut = strIn + spaces;

		}
		return strOut;
	}

	/**
	 * 以String类型返回错误抛出的堆栈信息
	 * 
	 * @param t
	 *            Throwable
	 * @return String
	 */
	public static String getStackTrace(Throwable t) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);

		t.printStackTrace(pw);
		return sw.toString();
	}

	/**
	 * 转换字符串第一个字符为大写
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String getStrByUpperFirstChar(String str) {
		try {
			return str.substring(0, 1).toUpperCase() + str.substring(1);
		} catch (Exception e) {
			return "";
		}

	}

	/**
	 * 转换字符串第一个字符为小写
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String getStrByLowerFirstChar(String str) {
		try {
			return str.substring(0, 1).toLowerCase() + str.substring(1);
		} catch (Exception e) {
			return "";
		}

	}

	/**
	 * 通过字符串转换成相应的整型，并返回。
	 * 
	 * @param strValue
	 *            String 待转换的字符串
	 * @return int 转换完成的整型
	 * */
	public static int getStrToInt(String strValue) {
		if (null == strValue) {
			return 0;
		}
		int iValue = 0;
		try {
			iValue = new java.lang.Integer(strValue.trim()).intValue();
		} catch (Exception ex) {
			iValue = 0;
		}
		return iValue;
	}

	/**
	 * 通过字符串转换成相应的DOUBLE，并返回。
	 * 
	 * @param strValue
	 *            String 待转换的字符串
	 * @return double 转换完成的DOUBLE
	 * */
	public static double getStrToDouble(String strValue) {
		if (null == strValue) {
			return 0;
		}
		double dValue = 0;
		try {
			dValue = Double.parseDouble(strValue.trim());
		} catch (Exception ex) {
			dValue = 0;
		}
		return dValue;
	}

	/**
	 * 通过字符串转换成相应的短整型，并返回。
	 * 
	 * @param strValue
	 *            String 待转换的字符串
	 * @return short 转换完成的短整型
	 * */
	public static short getStrToShort(String strValue) {
		if (null == strValue) {
			return 0;
		}
		short iValue = 0;
		try {
			iValue = new java.lang.Short(strValue.trim()).shortValue();
		} catch (Exception ex) {
			iValue = 0;
		}
		return iValue;
	}

	/**
	 * 通过字符串转换成相应的长整型，并返回。
	 * 
	 * @param strValue
	 *            String 待转换的字符串
	 * @return long 转换完成的长整型
	 * */
	public static long getStrToLong(String strValue) {
		if (null == strValue) {
			return 0;
		}
		long lValue = 0;
		try {
			lValue = new java.lang.Long(strValue.trim()).longValue();
		} catch (Exception ex) {
			lValue = 0;
		}
		return lValue;
	}

	public static String toLengthForEn(String str, int length) {
		if (null != str) {
			if (str.length() <= length) {
				return str;
			} else {
				str = str.substring(0, length - 2);
				str = str + "..";
				return str;
			}
		} else {
			return "";
		}
	}

	/**
	 * 降字符串转换成给定长度的字符串，如超出的话截断，并在最后以两个点结尾
	 * 
	 * @param str
	 *            String
	 * @param length
	 *            int
	 * @return String
	 */
	public static String toLengthForIntroduce(String str, int length) {
		str = delTag(str);

		byte[] strByte = str.getBytes();
		int byteLength = strByte.length;
		char[] charArray;
		StringBuffer buff = new StringBuffer();
		if (byteLength > (length * 2)) {
			charArray = str.toCharArray();
			int resultLength = 0;
			for (int i = 0; i < charArray.length; i++) {
				resultLength += String.valueOf(charArray[i]).getBytes().length;
				if (resultLength > (length * 2)) {
					break;
				}
				buff.append(charArray[i]);

			}
			buff.append("..");
			str = buff.toString();
		}

		// str = replace(str, "'", "\\'");
		str = replace(str, "\"", "\\\"");
		str = replace(str, "，", ",");
		return str;

	}

	public static String delTag(String str) {
		str = str + "<>";
		StringBuffer buff = new StringBuffer();
		int start = 0;
		int end = 0;

		while (str.length() > 0) {
			start = str.indexOf("<");
			end = str.indexOf(">");
			if (start > 0) {
				buff.append(str.substring(0, start));
			}
			if (end > 0 && end <= str.length()) {
				str = str.substring(end + 1, str.length());
			} else {
				str = "";
			}

		}
		String result = buff.toString();

		while (result.startsWith(" ")) {

			result = result.substring(result.indexOf(" ") + 1, result.length());

		}
		return result;

	}

	public static final String replace(String line, String oldString,
			String newString) {
		if (line == null) {
			return null;
		}
		int i = 0;
		if ((i = line.indexOf(oldString, i)) >= 0) {
			char[] line2 = line.toCharArray();
			char[] newString2 = newString.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(line2.length);
			buf.append(line2, 0, i).append(newString2);
			i += oLength;
			int j = i;
			while ((i = line.indexOf(oldString, i)) > 0) {
				buf.append(line2, j, i - j).append(newString2);
				i += oLength;
				j = i;
			}
			buf.append(line2, j, line2.length - j);
			return buf.toString();
		}
		return line;

	}

	// Replace
	public static String Replace(String source, String oldString,
			String newString) {
		if (source == null) {
			return null;
		}
		StringBuffer output = new StringBuffer();
		int lengOfsource = source.length();
		int lengOfold = oldString.length();
		int posStart = 0;
		int pos;
		while ((pos = source.indexOf(oldString, posStart)) >= 0) {
			output.append(source.substring(posStart, pos));
			output.append(newString);
			posStart = pos + lengOfold;
		}
		if (posStart < lengOfsource) {
			output.append(source.substring(posStart));
		}
		return output.toString();
	}

	// 此函数前台使用中，请勿随便修改，不然会造成显示混乱(以前修改版本在下方注释中)
	public static String toHtml(String s) {
		s = Replace(s, "<", "&lt;");
		s = Replace(s, ">", "&gt;");
		s = Replace(s, "\t", "    ");
		s = Replace(s, "\r\n", "\n");
		s = Replace(s, "\n", "<br>");
		// s = Replace(s, " ", "&nbsp;");
		s = Replace(s, "'", "&#39;");
		s = Replace(s, "\"", "&quot;");
		s = Replace(s, "\\", "&#92;");
		s = Replace(s, "%", "％");
		// s = Replace(s, "&", "&amp;");
		return s;
	}

	// 逆
	public static String unHtml(String s) {

		// s = Replace(s, "&lt;", "<");
		// s = Replace(s, "&gt;", ">");
		// s = Replace(s, "    ", "\t");
		// s = Replace(s, "\n", "\r\n");
		s = Replace(s, "<br>", "\n");
		// s = Replace(s, "&nbsp;", " ");
		// s = Replace(s, "&amp;", "&");
		// s = Replace(s, "&#39;", "'");
		// s = Replace(s, "&#92;", "\\");
		// s = Replace(s, "％", "%");
		return s;
	}

	// 此函数后台使用中，请勿随便修改，不然会造成显示混乱(以前修改版本在下方注释中)
	public static String toHtmlBack(String s) {
		// 显示
		s = Replace(s, "&", "&amp;");
		s = Replace(s, "\\", "&#92;");
		s = Replace(s, "\"", "&quot;");
		s = Replace(s, "<", "&lt;");
		s = Replace(s, ">", "&gt;");
		s = Replace(s, "\t", "    ");
		s = Replace(s, "\r\n", "\n");
		// s = Replace(s, "\n", "<br>");
		// s = Replace(s, " ", "&nbsp;");
		// s = Replace(s, "'", "&#39;");
		// s = Replace(s, "%", "%");

		return s;
	}

	// 逆
	public static String unHtmlBack(String s) {
		s = Replace(s, "&lt;", "<");
		s = Replace(s, "&gt;", ">");
		s = Replace(s, "    ", "\t");
		s = Replace(s, "\n", "\r\n");
		s = Replace(s, "<br>", "\n");
		s = Replace(s, "&nbsp;", " ");
		s = Replace(s, "&amp;", "&");
		s = Replace(s, "&#39;", "'");
		s = Replace(s, "&#92;", "\\");
		s = Replace(s, "％", "%");
		return s;
	}

	// 判断是否含有中文，如果含有中文返回ture
	public static boolean containsChinese(String str)
			throws UnsupportedEncodingException {

		if (str.length() < (str.getBytes()).length)
			return true;

		return false;

	}

	public static boolean isEmpty(String str) {
		if("null".equalsIgnoreCase(str)){
			return true;
		}
		if (str == null)
			return true;
		return "".equals(str.trim());
	}

	/**
	 * 
	 * <br>
	 * <b>功能：</b>数字转换成字符串<br>
	 * <b>日期：</b> Jul 30, 2013 <br>
	 * 
	 * @param arrs
	 * @return
	 */
	public static String arrayToString(String[] arrs) {
		StringBuffer result = new StringBuffer("");
		if (arrs != null && arrs.length > 0) {
			for (int i = 0; i < arrs.length; i++) {

				if (!result.toString().equals("")) {
					result.append(",");
				}
				if (arrs[i] != null && !"".equals(arrs[i].trim())) {
					result.append(arrs[i]);
				}
			}
		}
		return result.toString();
	}

	/**
	 * 
	 * <br>
	 * <b>功能：</b>数字转换成字符串<br>
	 * <b>日期：</b> Jul 30, 2013 <br>
	 * 
	 * @param arrs
	 * @return
	 */
	public static String arrayToString(Object[] arrs) {
		StringBuffer result = new StringBuffer("");
		if (arrs != null && arrs.length > 0) {
			for (int i = 0; i < arrs.length; i++) {

				if (!result.toString().equals("")) {
					result.append(",");
				}
				if (arrs[i] != null && !"".equals(arrs[i].toString().trim())) {
					result.append(arrs[i]);
				}
			}
		}
		return result.toString();
	}

	/**
	 * 
	 * <br>
	 * <b>功能：</b>替换回车<br>
	 * <b>日期：</b> Oct 26, 2013 <br>
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceHuiche(String str) {
		String after = str.replaceAll("\r\n", "");
		return after;
	}

	/**
	 * 根据输入的长度截取字符串，单个单词超过输入长度的强制加<BR>
	 * 换行
	 * 
	 * @param str
	 *            输入的字符串
	 * @param len
	 *            输入的长度
	 * @return 处理过后的字符串
	 */
	public static String truncateStr(String str, int len) {
		if (str != null && !("").equalsIgnoreCase(str)) {

			String strs[] = str.split(" ");
			StringBuffer buff = new StringBuffer();
			if (strs.length > 0) {
				for (int i = 0; i < strs.length; i++) {
					StringBuffer temp = new StringBuffer();
					while (strs[i].length() > len) {
						temp.append(strs[i].substring(0, len) + "<BR>");
						strs[i] = strs[i].substring(len);
					}
					temp.append(strs[i]);
					buff.append(temp.toString() + " ");
				}

			}
			return buff.toString();
		} else {
			return "";
		}
	}

	public static String truncateStr2(String str, int len) {
		if (str != null && !("").equalsIgnoreCase(str) && len != 0) {
			String strs[] = str.split(" ");

			StringBuffer buff = new StringBuffer();
			for (int i = 0; i < strs.length; i++) {
				StringBuffer temp = new StringBuffer();
				String tempstr = "";
				while (strs[i].length() > len) {
					tempstr = strs[i].substring(0, len);
					tempstr = tempstr.replaceAll(" ", "&nbsp; ");
					tempstr = tempstr.replaceAll("<", "&lt; ");
					tempstr = tempstr.replaceAll("\n", "<br> ")
							.replaceAll("\"", "&quot; ")
							.replaceAll("'", "&#39; ");
					tempstr = tempstr + "<br>";
					temp.append(tempstr);

					strs[i] = strs[i].substring(len);
				}
				tempstr = strs[i];
				tempstr = tempstr.replaceAll(" ", "&nbsp; ");
				tempstr = tempstr.replaceAll("<", "&lt; ");
				tempstr = tempstr.replaceAll("\n", "<br> ")
						.replaceAll("\"", "&quot; ").replaceAll("'", "&#39; ");

				temp.append(tempstr);
				buff.append(temp.toString() + " ");
			}

			if (buff.length() > 0)
				return buff.toString().substring(0, buff.length() - 1);
			else
				return str;
		} else {
			return "";
		}
	}

	/**
	 * 编码转换，从unicode转换为GBK
	 * 
	 * @param str
	 *            输入字符串
	 * @return str编码转换后的字符串
	 * @throws UnsupportedEncodingException
	 */
	public static String unicodeToGB(String l_S_Source)
			throws UnsupportedEncodingException {
		String l_S_Desc = "";
		if (l_S_Source != null && !l_S_Source.trim().equals("")) {
			byte l_b_Proc[] = l_S_Source.getBytes("GBK");
			l_S_Desc = new String(l_b_Proc, "ISO8859_1");
		}
		return l_S_Desc;
	}

	/**
	 * 编码转换，从GBK转换为unicode
	 * 
	 * @param str
	 *            输入字符串
	 * @return str 编码转换后的字符串
	 * @throws UnsupportedEncodingException
	 */
	public static String GBToUnicode(String l_S_Source)
			throws UnsupportedEncodingException {
		String l_S_Desc = "";
		if (l_S_Source != null && !l_S_Source.trim().equals("")) {
			byte l_b_Proc[] = l_S_Source.getBytes("ISO8859_1");
			l_S_Desc = new String(l_b_Proc, "GBK");
		}
		return l_S_Desc;
	}

	/**
	 * Escapes a <code>String</code> according the JavaScript string literal
	 * escaping rules. The resulting string will not be quoted.
	 * 
	 * <p>
	 * It escapes both <tt>'</tt> and <tt>"</tt>. In additional it escapes
	 * <tt>></tt> as <tt>\></tt> (to avoid <tt>&lt;/script></tt>). Furthermore,
	 * all characters under UCS code point 0x20, that has no dedicated escape
	 * sequence in JavaScript language, will be replaced with hexadecimal escape
	 * (<tt>\x<i>XX</i></tt>).
	 */
	public static String javaScriptStringEnc(String s) {
		int ln = s.length();
		for (int i = 0; i < ln; i++) {
			char c = s.charAt(i);
			if (c == '"' || c == '\'' || c == '\\' || c == '>' || c < 0x20) {
				StringBuffer b = new StringBuffer(ln + 4);
				b.append(s.substring(0, i));
				while (true) {
					if (c == '"') {
						b.append("\\\"");
					} else if (c == '\'') {
						b.append("\\'");
					} else if (c == '\\') {
						b.append("\\\\");
					} else if (c == '>') {
						b.append("\\>");
					} else if (c < 0x20) {
						if (c == '\n') {
							b.append("\\n");
						} else if (c == '\r') {
							b.append("\\r");
						} else if (c == '\f') {
							b.append("\\f");
						} else if (c == '\b') {
							b.append("\\b");
						} else if (c == '\t') {
							b.append("\\t");
						} else {
							b.append("\\x");
							int x = c / 0x10;
							b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
							x = c & 0xF;
							b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
						}
					} else {
						b.append(c);
					}
					i++;
					if (i >= ln) {
						return b.toString();
					}
					c = s.charAt(i);
				}
			} // if has to be escaped
		} // for each characters
		return s;
	}

	private static StringUtil instance = null;

	public static synchronized StringUtil getInstance() {
		if (instance == null) {
			instance = new StringUtil();
		}
		return instance;
	}

	/**
	 * 将多个连续空格替换为一个,"a  b"-->"a b"
	 * 
	 * @param src
	 * @return
	 * @author WilliamLau
	 * @desc
	 */
	public static String trimContinuousSpace(String src) {
		return src.replaceAll("\\s+", " ");
	}

	public static String toFloatNumber(String num) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		nf.setMinimumFractionDigits(2);
		return nf.format(Double.parseDouble(num));
	}

	public static String toFloatNumber(Double num, int accuracy) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(accuracy);
		nf.setMinimumFractionDigits(accuracy);
		return nf.format(num);
	}

	public static String wcsUnescape(String str) {
		str = str.replace("#lt;", "<");
		str = str.replace("#gt;", ">");
		str = str.replace("#quot;", "\"");
		str = str.replace("#amp;amp;", "&");
		str = str.replace("#amp;", "&");
		str = str.replace("#039;", "'");
		return str;
	}

	/**
	 * 
	 * <br>
	 * <b>功能：</b>返回string型的字节数<br>
	 * <b>日期：</b> Sep 2, 2013 <br>
	 * 
	 * @param str
	 * @return
	 */
	public static int getByteLength(String str) {
		if (str == null) {
			return 0;
		}
		return str.getBytes().length;
	}

	/**
	 * 
	 * <br>
	 * <b>功能：</b>详细的功能描述<br>
	 * <b>日期：</b> Sep 2, 2013 <br>
	 * 
	 * @param str
	 *            字符
	 * @param limitLen
	 *            长度
	 * @return
	 */
	public static String getByteStr(String str, int limitLen) {
		StringBuffer sb = new StringBuffer();
		char[] chars = getNotNullStr(str).toCharArray();
		int len = 0;
		for (char c : chars) {
			len += getByteLength(String.valueOf(c));
			if (len <= limitLen) {
				sb.append(c);
			}
		}
		return sb.toString();

	}

	/****
	 * 获取固定长度的随机数
	 * 
	 * @param strLength
	 * @return
	 */
	public static String getFixLenthString(int length) {
		// 35是因为数组是从0开始的，26个字母+10个数字
		final int maxNum = 36;
		int i; // 生成的随机数
		int count = 0; // 生成的密码的长度
		char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		while (count < length) {
			// 生成随机数，取绝对值，防止生成负数，

			i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1

			if (i >= 0 && i < str.length) {
				pwd.append(str[i]);
				count++;
			}
		}

		return pwd.toString();
	}

	/**
	 * 获取UUID随机码
	 * 
	 * @return
	 */
	public static String generationUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().substring(0, 7);
	}

	/**
	 * 判断是否是数字，可以判断整数小数
	 * 
	 * @return
	 */
	public static Boolean isNumer(String str) {
		// 可以判断整数小数
		return str.matches("\\d+\\.?\\d*");
	}

	// 生成uuid随机码
	public static String getUUID() {
		return UUID.randomUUID().toString().substring(0, 36);
	}

	// 生成6位随机验证码
	public static String getCaptcha() {
		return String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
	}

	// 检查对象是否为空
	public static boolean isEmpty(Object pObj) {
		if (pObj == null)
			return true;
		if (pObj == "")
			return true;
		if (pObj instanceof String) {
			if (((String) pObj).length() == 0) {
				return true;
			}
		} else if (pObj instanceof Collection) {
			if (((Collection) pObj).size() == 0) {
				return true;
			}
		} else if (pObj instanceof Map) {
			if (((Map) pObj).size() == 0) {
				return true;
			}
		}
		return false;
	}

	// 将 s 进行 BASE64 编码
	public static String getBASE64(String s) {
		if (s == null)
			return null;
		return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
	}

	// 将 s 进行 BASE64 编码
	public static String getBASE64(String s, String encoding) {
		if (s == null)
			return null;
		try {
			return (new sun.misc.BASE64Encoder()).encode(s.getBytes(encoding));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 将 BASE64 编码的字符串 s 进行解码
	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		;
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}


	/******************************************************************/

	/**
	 * 检查指定的字符串列表是否不为空。
	 */
	public static boolean areNotEmpty(String... values) {
		boolean result = true;
		if (values == null || values.length == 0) {
			result = false;
		} else {
			for (String value : values) {
				result &= !isEmpty(value);
			}
		}
		return result;
	}

	/**
	 * 过滤不可见字符
	 */
	public static String stripNonValidXMLCharacters(String input) {
		if (input == null || ("".equals(input)))
			return "";
		StringBuilder out = new StringBuilder();
		char current;
		for (int i = 0; i < input.length(); i++) {
			current = input.charAt(i);
			if ((current == 0x9) || (current == 0xA) || (current == 0xD)
					|| ((current >= 0x20) && (current <= 0xD7FF))
					|| ((current >= 0xE000) && (current <= 0xFFFD))
					|| ((current >= 0x10000) && (current <= 0x10FFFF)))
				out.append(current);
		}
		return out.toString();
	}

	/**
	 * 分转换为元，保留两位小数
	 * 
	 * @param toChangePrice
	 * @return
	 */
	public static String priceChange(String toChangePrice) {
		try {
			toChangePrice = toChangePrice.replace("-", "");
			int index = toChangePrice.indexOf(".");
			if (index != -1) {
				toChangePrice = toChangePrice.substring(0, index);
			}
			long tempPrice = Long.valueOf(toChangePrice);
			long price = tempPrice % 100;
			StringBuilder sb = new StringBuilder();
			if (price < 10) {
				sb.append(tempPrice / 100).append(".0").append(price);
				return sb.toString();
			}
			sb.append(tempPrice / 100).append(".").append(price);
			return sb.toString();
		} catch (Exception e) {
			return "0";
		}
	}

	/**
	 * 元转换为分，保留元两位小数
	 * 
	 * @param toChangePrice
	 * @return
	 */
	public static String priceChangeF(String toChangePriceY) {
		String result = "0";
		try {
			if (isEmpty(toChangePriceY)) {
				return result;
			}
			float scale = Float.parseFloat(toChangePriceY);
			DecimalFormat fnum = new DecimalFormat("##0.000");
			result = fnum.format(scale);
			result = String.valueOf(Float.parseFloat(result) * 100);
			result = result.substring(0, result.indexOf("."));
			return result;
		} catch (Exception e) {
			return "0";
		}
	}

	
	/**
	 * 判断时间格式
	 * @param date
	 * @return
	 */
	public static boolean validateDatetimeFormat(String date){
		date=date.trim();
		String yyyyMMddHHmmss = "[0-9]{4}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}";//yyyyMMddHHmmss
		boolean datea1 = Pattern.compile(yyyyMMddHHmmss).matcher(date).matches();
		if(datea1){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 组装个推消息
	 * @param title
	 * @param content
	 * @param actionType
	 * @param link
	 * @param time
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getTransmissionData(String title, String content, String actionType, String link, long time,Map<String,Object> dataExtend) {

        Map data = new HashMap();
        data.put("TITLE", title);
        data.put("CONTENT", content);
        data.put("ACTION_TYPE", actionType);
        data.put("LINK", link);
        data.put("TIME", time);//推送时间
        if(dataExtend != null) {
        	data.put("DATA_EXTEND", dataExtend);//扩展字段
        }

        Map rtn = new HashMap();
        rtn.put("errcode", 1);
        rtn.put("msg", "SUCCESS");
        rtn.put("data", data);
        rtn.put("timestamp", DateUtil.getTime(new Date()));
        return JSONParser.obj2Json(rtn);
    }
	/**
	 * douboe类型字符串转int
	 */
	public static Integer parseInt(String str) {
		if (null == str) {
			return 0;
		}
		int iValue = 0;
		try {
			iValue = new Double(str.trim()).intValue();
		} catch (Exception ex) {
			iValue = 0;
		}
		return iValue;
	}
	
	public static String strFormat(String format, Object... args) {
		return String.format(format,args);
	}
	
	public static void main(String[] args) {
		System.out.println(getFromBASE64("5rWZQTEyMzQ1"));
	}
}
